#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include <cstring>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "Comm.hpp"
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cout << "Incorrect use, correct use: " << endl
             << "    ./server ip port" << endl;
    }

    // 本地通过文件获取已有的服务
    unordered_map<string, bool> hash;
    ifstream in("oplist.txt");
    string inop;
    while (in >> inop)
    {
        hash[inop] = true;
    }
    in.close();

    string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        cout << errno << ": " << strerror(errno) << endl;
        exit(Sock_Err);
    }

    // 这里仍不需要显式的bind
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    inet_pton(AF_INET, serverip.c_str(), &serveraddr.sin_addr);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(serverport);

    int ret = connect(sockfd, CV(serveraddr), sizeof(serveraddr)); // 在connect时会自动bind

    char oplist[1024];
    ssize_t n = read(sockfd, oplist, sizeof(oplist) - 1); // 获取服务列表
    oplist[n] = 0;
    cout << oplist << endl;

    cout << "请选择一个操作：";
    string op;
    getline(cin, op);
    n = write(sockfd, op.c_str(), op.size()); // 向服务端发送操作符
    if (n > 0)
    {
        string message;
        if (op != "ping" && hash.count(op))
        {
            cout << "请输入内容：";
            getline(cin, message);
            n = write(sockfd, message.c_str(), message.size()); // 向服务端发送被操作内容
            if (n > 0)
            {
                char buffer[1024];
                n = read(sockfd, buffer, sizeof(buffer) - 1); // 读取结果
                buffer[n] = 0;
                cout << "结果：" << buffer << endl;
            }
            else
            {
                cout << errno << ": " << strerror(errno) << endl;
                close(sockfd);
                exit(Write_Err);
            }
        }
        else // 对于未知服务和ping做特殊处理
        {
            char buffer[1024];
            n = read(sockfd, buffer, sizeof(buffer) - 1);
            buffer[n] = 0;
            cout << buffer << endl;
        }
    }
    else
    {
        cout << errno << ": " << strerror(errno) << endl;
        close(sockfd);
        exit(Write_Err);
    }

    close(sockfd);
    return 0;
}

// int main(int argc, char *argv[])
// {
//     if (argc != 3)
//     {
//         cout << "Incorrect use, correct use: " << endl
//              << "    ./server ip port" << endl;
//     }

//     string serverip = argv[1];
//     uint16_t serverport = stoi(argv[2]);

//     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
//     if (sockfd < 0)
//     {
//         cout << errno << ": " << strerror(errno) << endl;
//         exit(Sock_Err);
//     }

//     // 这里仍不需要显式的bind
//     struct sockaddr_in serveraddr;
//     memset(&serveraddr, 0, sizeof(serveraddr));
//     inet_pton(AF_INET, serverip.c_str(), &serveraddr.sin_addr);
//     serveraddr.sin_family = AF_INET;
//     serveraddr.sin_port = htons(serverport);

//     int n = connect(sockfd, CV(serveraddr), sizeof(serveraddr)); // 在connect时会自动bind

//     char buffer[1024];
//     while (true)
//     {
//         string inbuffer;
//         cout << "Please Enter# ";
//         getline(cin, inbuffer);
//         ssize_t n = write(sockfd, inbuffer.c_str(), inbuffer.size());
//         if (n > 0)
//         {
//             ssize_t n = read(sockfd, buffer, sizeof(buffer) - 1);
//             if (n < 0)
//             {
//                 cout << errno << ": " << strerror(errno) << endl;
//                 exit(Read_Err);
//             }
//             else if (n == 0)
//             {
//                 cout << "server quit..." << endl;
//                 break;
//             }
//             else
//             {
//                 buffer[n] = 0;
//                 cout << "server say# " << buffer << endl;
//             }
//         }
//         else
//         {
//             cout << errno << ": " << strerror(errno) << endl;
//             exit(Write_Err);
//         }
//     }

//     close(sockfd);
//     return 0;
// }